﻿using DatabaseCore;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SystemCore.Converter;


namespace Export.Web.Ui.pages
{
    public partial class ImportNDPrice : System.Web.UI.Page
    {
       
        private const string C_KEY_ORDER = "Order";
        private OrderDao orderProvider = new OrderDao();
        private TransactionDao transectionProvider = new TransactionDao();
        private OrderDetailDao orderDetailProvider = new OrderDetailDao();


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bthUploadExcel_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentType.Contains("spreadsheetml"))
            {
                string pathsave = "/Upload/" + DateTime.Today.ToString("dd-MM-yyyy") + "/";
                string folderpath = System.Web.HttpContext.Current.Server.MapPath(pathsave);
                if (!System.IO.Directory.Exists(folderpath))
                {
                    System.IO.Directory.CreateDirectory(folderpath);
                }
                string _username = HttpContext.Current.Session["Username"] == null ? "" : HttpContext.Current.Session["Username"].ToString();
                FileUpload1.SaveAs(folderpath + FileUpload1.FileName);
                
                    tbl_Order order = new tbl_Order();
                    List<tbl_OrderDetail> orderDetails = new List<tbl_OrderDetail>();
                    
                    string NewOrderNo = GenerateOrderNo();
                    string matDuplicate = "";
                    bool result = Helper.ExcelHelper.GetOrderData(folderpath + FileUpload1.FileName, NewOrderNo, _username, out order, out orderDetails, out matDuplicate);
                    if (result)
                    {
                        order.OrderNo = NewOrderNo;

                        orderProvider.Insert(order);
                        orderDetailProvider.Insert(orderDetails);

                        var transection = orderDetails.ConvertAll(new Converter<tbl_OrderDetail, tbl_Transaction>(ConverterHelper.OrderDetailToTransaction));
                        transectionProvider.InsertCollection(transection);

                        LiteralReport.Text = string.Format("Import success with {0} items", orderDetails.Count);
                        btnCloseButton.Visible = true;
                        bthUploadExcel.Visible = false;

                    }
                    else
                    {
                        LiteralReport.Text = string.Format("Import ไม่สำเร็จ เนื่องจากมี Material " + matDuplicate + " ซ้ำกัน");
                        btnCloseButton.Visible = true;
                        bthUploadExcel.Visible = false;
                    }
            }
            else
            {
                LiteralReport.Text = string.Format("Please upload the correct file");
            }
        }


        private string GenerateOrderNo()
        {
            GenerateNoDao generateNoProvider = new GenerateNoDao();
            int runningNo = generateNoProvider.GenerateNo(C_KEY_ORDER);
            string orderNo = string.Format("{0}{1}", DateTime.Now.ToString("ddMMyy", new CultureInfo("en-US")), runningNo.ToString("000000"));
            return orderNo;
        }
    }
}